<?php
/**
 * 
 * Authentication model
 * 
 * @category Abovo
 * 
 * @package Abovo_Model
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 */

/**
 * 
 * Authentication model
 * 
 * @category Abovo
 * 
 * @package Abovo_Model
 * 
 */
class Abovo_Model_Auth extends Abovo_Sql_Table {
    
    /**
     * 
     * DataFilter class to use
     * 
     * @var string
     * 
     */
    protected $_filter_class = 'Abovo_Model_Auth_DataFilter';
    
    /**
     * 
     * Fetch user's info by handle (username)
     * 
     * @param string $handle User handle
     * 
     * @return Solar_Sql_Row|bool If handle exists
     * otherwise a boolean false
     * 
     */
    public function fetchByHandle($handle)
    {
        $sel = Solar::factory('Solar_Sql_Select');
        
        $sel->from($this->_name, '*')
            ->where('handle = ?', $handle);
        
        // fetch row object
        $user = $sel->fetch('row');
        
        if (count($user) > 0) {
            return $user;
        }
        
        // handle does not exist!
        return false;
    }
    
    /**
     * 
     * Inserts one user to the table
     * 
     * In addition to parent method this
     * changes the password from plaintext to md5.
     * 
     * @param array $data Data to insert
     * 
     * @return array Data as inserted
     * 
     */
    public function insert($data)
    {
        // Check if password exists and change to md5 if it does.
        if (array_key_exists('password', $data)) {
            $data['password'] = hash('md5', $data['password']);
        }
        
        return parent::insert($data);
    }
    
    /**
     * 
     * Checks if handle is already taken
     * 
     * @param string User handle
     * 
     * @return bool True if handle is unique,
     * false if handle is empty or is not unique.
     * 
     */
    public function validateUniqueHandle($handle)
    {
        // is it empty?
        if (! empty($handle)) {
            return (bool) ! $this->fetchByHandle((string) $handle);
        }
        
        // empty handle, this is NOT allowed!
        return false;
    }
    
    /**
     * 
     * Checks if password is correct for handle
     *
     * @param string $handle User handle
     * 
     * @param string $passwd Password to match against
     * 
     * @return bool True if handle exists
     * and passwords match, otherwise false.
     * 
     */
    public function isValidPassword($handle, $passwd)
    {
        // fetch user by handle
        $user = $this->fetchByHandle($handle);
        
        // handle exists?
        if ($user === false) {
            return false;
        }
        
        // do they match?
        if ($user->password === hash('md5', $passwd)) {
            return true;
        }
        
        return false;
    }
    
    /**
     * 
     * Set up
     *
     * @return void
     * 
     */
    protected function _setup()
    {
        // Table name
        $this->_name = 'auth';
        
        $filter = Solar::factory('Abovo_Model_Auth_DataFilter');
        
        // Username
        $this->_col['handle'] = array(
            'type'    => 'varchar',
            'size'    => 15,
            'require' => true,
            'filter'  => 'validateHandle',
        );
        
        // Password
        $this->_col['password'] = array(
            'type'    => 'varchar',
            'size'    => 32,
            'require' => true,
        );
        
        // Indexes
        $this->_idx = array(
            'handle' => 'normal',
        );
        
        // Make sure sql is available
        if (! Solar::isRegistered('sql')) {
            Solar::register('sql', Solar::factory('Solar_Sql'));
        }
    }
}
